﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM
{
    public partial class UploadPopup : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }

            if (!IsPostBack)
            {
                ClearScreen();
            }
        }


        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                string lsInternalID = string.Empty;
                string savedFileName01 = string.Empty, savedFileName02 = string.Empty, savedFileName03 = string.Empty, savedFileName04 = string.Empty, savedFileName05 = string.Empty;

                DataTable dtMasterFile = new DataTable("tbl_masterfile");
                dtMasterFile.Columns.Add("internal_id");
                dtMasterFile.Columns.Add("title");
                dtMasterFile.Columns.Add("publish_date");
                dtMasterFile.Columns.Add("valid_date");
                dtMasterFile.Columns.Add("isinvalid");
                dtMasterFile.Columns.Add("document_type");
                dtMasterFile.Columns.Add("dept");
                dtMasterFile.Columns.Add("file_name");
                dtMasterFile.Columns.Add("file_type");
                dtMasterFile.Columns.Add("file_date");
                dtMasterFile.Columns.Add("file_by");
                dtMasterFile.Columns.Add("ispublic");
                DataRow dr = null;

                if (FileUploadControl_1.HasFile) // File 01
                {
                    dr = dtMasterFile.NewRow();
                    lsInternalID = Guid.NewGuid().ToString();
                    
                    dr["internal_id"] = lsInternalID;
                    dr["title"] = txtTitle1.Text.Trim();
                    if( string.IsNullOrEmpty(txtPublishDate_01.Text.Trim()))
                        dr["publish_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    else
                        dr["publish_date"] = Utils.AppConstants.ConvertToDate(txtPublishDate_01.Text.Trim()).ToString("MM/dd/yyyy");

                    if(string.IsNullOrEmpty(txtValid2Date_01.Text.Trim()))
                        dr["valid_date"] = DateTime.MaxValue.ToString("MM/dd/yyyy");
                    else
                        dr["valid_date"] = Utils.AppConstants.ConvertToDate(txtValid2Date_01.Text.Trim()).ToString("MM/dd/yyyy");
                    dr["isinvalid"] = 0; // valid
                    dr["document_type"] = ddlDocType_1.SelectedValue;
                    dr["dept"] = ddlDept_1.SelectedValue;

                    dr["file_name"] = file_name1.Value;
                    dr["file_type"] = file_type1.Value;
                    dr["file_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    dr["file_by"] = Session[Utils.AppConstants.ID_User].ToString();
                    dr["ispublic"] = ddlOption_1.SelectedValue;
                    dtMasterFile.Rows.Add(dr);
                    // Save file.
                    savedFileName01 = Server.MapPath("~/SupportDocs/Documents/" + lsInternalID + "_" + FileUploadControl_1.FileName);
                }

                if (FileUploadControl_2.HasFile) // File 02
                {
                    dr = dtMasterFile.NewRow();
                    lsInternalID = Guid.NewGuid().ToString();

                    dr["internal_id"] = lsInternalID;
                    dr["title"] = txtTitle2.Text.Trim();
                    if (string.IsNullOrEmpty(txtPublishDate_02.Text.Trim()))
                        dr["publish_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    else
                        dr["publish_date"] = Utils.AppConstants.ConvertToDate(txtPublishDate_02.Text.Trim()).ToString("MM/dd/yyyy");

                    if (string.IsNullOrEmpty(txtValid2Date_02.Text.Trim()))
                        dr["valid_date"] = DateTime.MaxValue.ToString("MM/dd/yyyy");
                    else
                        dr["valid_date"] = Utils.AppConstants.ConvertToDate(txtValid2Date_02.Text.Trim()).ToString("MM/dd/yyyy");
                    dr["isinvalid"] = 0; // valid
                    dr["document_type"] = ddlDocType_2.SelectedValue;
                    dr["dept"] = ddlDept_2.SelectedValue;

                    dr["file_name"] = file_name2.Value;
                    dr["file_type"] = file_type2.Value;
                    dr["file_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    dr["file_by"] = Session[Utils.AppConstants.ID_User].ToString();
                    dr["ispublic"] = ddlOption_2.SelectedValue;
                    dtMasterFile.Rows.Add(dr);
                    // Save file.
                    savedFileName02 = Server.MapPath("~/SupportDocs/Documents/" + lsInternalID + "_" + FileUploadControl_2.FileName);
                }

                if (FileUploadControl_3.HasFile) // File 03
                {
                    dr = dtMasterFile.NewRow();
                    lsInternalID = Guid.NewGuid().ToString();

                    dr["internal_id"] = lsInternalID;
                    dr["title"] = txtTitle3.Text.Trim();
                    if (string.IsNullOrEmpty(txtPublishDate_03.Text.Trim()))
                        dr["publish_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    else
                        dr["publish_date"] = Utils.AppConstants.ConvertToDate(txtPublishDate_03.Text.Trim()).ToString("MM/dd/yyyy");

                    if (string.IsNullOrEmpty(txtValid2Date_03.Text.Trim()))
                        dr["valid_date"] = DateTime.MaxValue.ToString("MM/dd/yyyy");
                    else
                        dr["valid_date"] = Utils.AppConstants.ConvertToDate(txtValid2Date_03.Text.Trim()).ToString("MM/dd/yyyy");
                    dr["isinvalid"] = 0; // valid
                    dr["document_type"] = ddlDocType_3.SelectedValue;
                    dr["dept"] = ddlDept_3.SelectedValue;

                    dr["file_name"] = file_name3.Value;
                    dr["file_type"] = file_type3.Value;
                    dr["file_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    dr["file_by"] = Session[Utils.AppConstants.ID_User].ToString();
                    dr["ispublic"] = ddlOption_3.SelectedValue;
                    dtMasterFile.Rows.Add(dr);
                    // Build file name.
                    savedFileName03 = Server.MapPath("~/SupportDocs/Documents/" + lsInternalID + "_" + FileUploadControl_3.FileName);
                }

                if (FileUploadControl_4.HasFile) // File 04
                {
                    dr = dtMasterFile.NewRow();
                    lsInternalID = Guid.NewGuid().ToString();

                    dr["internal_id"] = lsInternalID;
                    dr["title"] = txtTitle4.Text.Trim();
                    if (string.IsNullOrEmpty(txtPublishDate_04.Text.Trim()))
                        dr["publish_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    else
                        dr["publish_date"] = Utils.AppConstants.ConvertToDate(txtPublishDate_04.Text.Trim()).ToString("MM/dd/yyyy");

                    if (string.IsNullOrEmpty(txtValid2Date_04.Text.Trim()))
                        dr["valid_date"] = DateTime.MaxValue.ToString("MM/dd/yyyy");
                    else
                        dr["valid_date"] = Utils.AppConstants.ConvertToDate(txtValid2Date_04.Text.Trim()).ToString("MM/dd/yyyy");
                    dr["isinvalid"] = 0; // valid
                    dr["document_type"] = ddlDocType_4.SelectedValue;
                    dr["dept"] = ddlDept_4.SelectedValue;

                    dr["file_name"] = file_name4.Value;
                    dr["file_type"] = file_type4.Value;
                    dr["file_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    dr["file_by"] = Session[Utils.AppConstants.ID_User].ToString();
                    dr["ispublic"] = ddlOption_4.SelectedValue;
                    dtMasterFile.Rows.Add(dr);
                    // Build file name.
                    savedFileName04 = Server.MapPath("~/SupportDocs/Documents/" + lsInternalID + "_" + FileUploadControl_4.FileName);
                }

                if (FileUploadControl_5.HasFile) // File 05
                {
                    dr = dtMasterFile.NewRow();
                    lsInternalID = Guid.NewGuid().ToString();

                    dr["internal_id"] = lsInternalID;
                    dr["title"] = txtTitle5.Text.Trim();
                    if (string.IsNullOrEmpty(txtPublishDate_05.Text.Trim()))
                        dr["publish_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    else
                        dr["publish_date"] = Utils.AppConstants.ConvertToDate(txtPublishDate_05.Text.Trim()).ToString("MM/dd/yyyy");

                    if (string.IsNullOrEmpty(txtValid2Date_05.Text.Trim()))
                        dr["valid_date"] = DateTime.MaxValue.ToString("MM/dd/yyyy");
                    else
                        dr["valid_date"] = Utils.AppConstants.ConvertToDate(txtValid2Date_05.Text.Trim()).ToString("MM/dd/yyyy");
                    dr["isinvalid"] = 0; // valid
                    dr["document_type"] = ddlDocType_5.SelectedValue;
                    dr["dept"] = ddlDept_5.SelectedValue;

                    dr["file_name"] = file_name5.Value;
                    dr["file_type"] = file_type5.Value;
                    dr["file_date"] = DateTime.Now.ToString("MM/dd/yyyy");
                    dr["file_by"] = Session[Utils.AppConstants.ID_User].ToString();
                    dr["ispublic"] = ddlOption_5.SelectedValue;
                    dtMasterFile.Rows.Add(dr);
                    // Build file name.
                    savedFileName05 = Server.MapPath("~/SupportDocs/Documents/" + lsInternalID + "_" + FileUploadControl_5.FileName);
                }

                //Save file to folder
                if (!string.IsNullOrEmpty(savedFileName01)) FileUploadControl_1.SaveAs(savedFileName01);
                if (!string.IsNullOrEmpty(savedFileName02)) FileUploadControl_2.SaveAs(savedFileName02);
                if (!string.IsNullOrEmpty(savedFileName03)) FileUploadControl_3.SaveAs(savedFileName03);
                if (!string.IsNullOrEmpty(savedFileName04)) FileUploadControl_4.SaveAs(savedFileName04);
                if (!string.IsNullOrEmpty(savedFileName05)) FileUploadControl_5.SaveAs(savedFileName05);
                // Save information file to Database
                Transaction ts = new Transaction();
                DataSet dsRtn = ts.CreateMasterFile(Utils.AppConstants.VSM, dtMasterFile, false);
                if ((int)dsRtn.Tables[0].Rows[0]["ErrCode"] != 0)
                {
                    lblError.Text = GeneralFunctions.UrlFullEncode(dsRtn.Tables[0].Rows[0]["ErrMsg"].ToString());
                    lblError.Visible = true;
                }
                else
                {
                    lblError.Text = "Operation complete sucessful!";
                    lblError.ForeColor = System.Drawing.Color.Blue;
                    lblError.Visible = true;
                    ClearScreen();
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('UploadFile');", true);
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
                lblError.Visible = true;
            }
        }

        private void ClearScreen()
        {
            txtTitle1.Text = txtTitle2.Text = txtTitle3.Text = txtTitle4.Text = txtTitle5.Text =
            file_name1.Value = file_name2.Value = file_name3.Value = file_name4.Value = file_name5.Value =
            file_type1.Value = file_type2.Value = file_type3.Value = file_type4.Value = file_type5.Value = 
            txtValid2Date_01.Text = txtValid2Date_02.Text = txtValid2Date_03.Text = txtValid2Date_04.Text = txtValid2Date_05.Text = string.Empty;

            ddlOption_1.SelectedIndex = ddlOption_2.SelectedIndex = ddlOption_3.SelectedIndex = ddlOption_4.SelectedIndex = ddlOption_5.SelectedIndex =
            ddlDocType_1.SelectedIndex = ddlDocType_2.SelectedIndex = ddlDocType_3.SelectedIndex = ddlDocType_4.SelectedIndex = ddlDocType_5.SelectedIndex =
            ddlDept_1.SelectedIndex = ddlDept_2.SelectedIndex = ddlDept_3.SelectedIndex = ddlDept_4.SelectedIndex = ddlDept_5.SelectedIndex = 0;

            txtPublishDate_01.Text = txtPublishDate_02.Text = txtPublishDate_03.Text = txtPublishDate_04.Text = txtPublishDate_05.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }
    }
}